博客
关于我
system()函数的setuid程序得不到root的权限;system()函数的suid失效问题
阅读量:201 次
发布时间:2019-02-28

本文共 2177 字,大约阅读时间需要 7 分钟。

system()函数的setuid程序得不到root的权限;system()函数的suid失效问题

相关背景:

老师上课讲了setuid程序;但是在调用system()函数时setuid不起作用

相关文章:


问题描述:

对调用system()函数的程序catall.c 如下;当设置根用户setuid权限后,理想情况下这个程序应该运行 /bin/cat 程序,可以查看所有的文件,但不能写入任何文件

/* catall.c */#include 
#include
#include
int main(int argc, char const *argv[]){ char *cat = "/bin/cat"; if(argc<2) { printf("please type a file name.\n"); return 1; } char *command = malloc(strlen(cat) + strlen(argv[1] + 2)); sprintf(command,"%s %s",cat,argv[1]); system(command); return 0;}
ppt 上的运行示例:可以成功拿到root权限,查看/etc/shadow 文件

在这里插入图片描述

实际运行结果:还是显示permission denied。。。明明前面 ls -l 显示属主是 root 且运行位是 s。。。说明这里的 setuid不起作用了。

在这里插入图片描述

最离谱的是,同一程序的系统调用,使用不同的函数,结果不一样,函数 execve() 的系统调用可以照常拿到 root 权限

在这里插入图片描述
在这里插入图片描述


原因分析:

linux 上直接 man system,得到的解释是:

DESCRIPTION   The  system()  library  function uses fork(2) to create a child process that   executes the shell command specified in command using execl(3) as follows:       execl("/bin/sh", "sh", "-c", command, (char *) NULL);   system() returns after the command has been completed.   During execution of the command, SIGCHLD will be  blocked,  and  SIGINT  and   SIGQUIT will be ignored, in the process that calls system().  (These signals   will be handled according to their defaults inside the  child  process  that   executes command.)   If  command  is  NULL,  then  system() returns a status indicating whether a   shell is available on the system.

应该就是fork一个子进程,然后调用execl("/bin/sh", “sh”, “-c”, command, (char *) NULL); 去执行命令。

直接用 execl() 函数试一下

在这里插入图片描述

在这里插入图片描述

下面显示有euid=0,说明execve() 拿到 root 执行权限,而上面 execl() 跟system() 一样没有执行位权限

这里再试一下 execl() 函数直接用 /bin/id 不通过 /bin/shell;
在这里插入图片描述
在这里插入图片描述
execl() 函数拿到了 root 的执行权限 euid=0;所以问题应该在/bin/sh 上。应该是setuid程序不能将root权限传给/bin/sh;很容易理解,因为这样非常不安全

老师PPT中有解释

在Ubuntu 16.04中,/bin/sh指向/bin/dash,这有一个对策:当它在set-uid进程中执行时,它将删除特权。因此,在以上的攻击中,我们只会得到一个普通shell

A Note

• In Ubuntu 16.04, /bin/sh points to /bin/dash, which has a countermeasure
• It drops privilege when it is executed inside a set-uid process
• Therefore, we will only get a normal shell in the attack on the previous slide


解决方案:

Do the following to remove the countermeasure

在这里插入图片描述
这里将 /bin/sh指向 /bin/zsh;
zsh是一个功能强大的shell,笔者由于没有安装zsh,就没有试过。安装之后应该解决完成问题

转载地址:http://hfhc.baihongyu.com/

你可能感兴趣的文章
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>